// Ofri Keidar 302933981 group 05

package view.useBeans;

/**
 * Bean class holding product's data
 */
public class ProductData {

	// product's details
	String name;
	String price;
	
	// setters

	/**
	 * Sets product's name
	 * @param name- name to set
	 */
	public void setName(String name) { this.name = name; }

	/**
	 * Sets product's price
	 * @param price- price to set
	 */
	public void setPrice(String price) { this.price = price; }
	
	// getters
	
	/**
	 * Returns product's name
	 * @return name attribute
	 */
	public String getName() { return name; }
	
	/**
	 * Returns product's price
	 * @return price attribute
	 */
	public String getPrice() { return price; }
	
	// utilities
	
	/**
	 * Checks if data had been already set.
	 * Data is set if members are not null nor empty strings
	 * @return True if data is set, False if not
	 */
	public boolean isDataSet() {

		// validate name
		boolean isNameSet = true;
		if (name == null) { isNameSet = false;} 
		else if (name.equals("")) { isNameSet = false; }
		
		// validate price
		boolean isPriceSet = true;
		if (price == null) { isNameSet = false;} 
		else if (price.equals("")) { isNameSet = false; }
		
		// check if parameters are set
		return isNameSet && isPriceSet;
	}
	
	/**
	 * Returns price's numeric value.
	 * If data is not set or price string does 
	 * not contain a positive numeric value, returns Double.Nan
	 * @return price's numeric value
	 */
	public double getPriceValue() {
		
		// check if data is set
		if (!isDataSet()) { return Double.NaN; }
		
		// try parsing price
		try {

			// parse value
			Double parsedVal = Double.parseDouble(price);
			
			// check if parsed a numeric value
			if (Double.isNaN(parsedVal)) { return Double.NaN; }
			
			// check if parsed number is positive
			if (parsedVal <= 0) { return Double.NaN; }
			
			// parsed value is a positive number
			return parsedVal;
			
		} catch (NumberFormatException e) {
			
			// content is not numeric
			return Double.NaN;
		}
	}
	
	/**
	 * Clears attributes
	 */
	public void clear() {
		
		// clear attributes
		name = "";
		price = "";
	}
}